<?php

//Deal expires at 11:30am
include '../../../wp-blog-header.php';

$url =    'http://feeds.omgeu.com/data/feed.aspx?hash=75c09cf810c644119990cdbe0d6587de&page=1';
$brand_id = '1118';
function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$vogue_deals = xml2array($curl_json_output);
shuffle($vogue_deals[ProductFeed][Products][Product]);
$vogue_deals = array_slice($vogue_deals[ProductFeed][Products][Product], 0, 1);
/*
echo '<pre>';
print_r($vogue_deals[ProductFeed][Products][Product]);
echo '</pre>';
exit();
*/

foreach($vogue_deals as $key => $value){
    //echo '<pre>';
    //print_r($value);
    //echo '</pre>';
   //exit();
    $columns_deal['name'] = pods_sanitize($value['Name']);
    $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    $ext_img_url = $value['SmallImage'];
    $deal = new Pod('deal');
    $where = 't.camp_link = "'.$columns_deal['camp_link'].'" AND t.daily = 1';
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
    
    if($dealsFound == 0)
    {
      unset($ids);
      unset($current_cities);
      $City = new Pod('city');
    	$City->findRecords('name ASC', -1);
    	while($City->fetchRecord())
    	{
      	$ids  = $City->get_field('id');
      	$current_cities[] = $ids;
    	}
      $current_cities = implode(',',$current_cities);
      $columns_deal['city'] = $current_cities;
      
      $shortened_url = make_bitly_url($value['URL']);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($value['URL']);
      }
      $columns_deal['extlink'] = $shortened_url;

      date_default_timezone_set('Asia/Kolkata');
      $columns_deal['camp_end'] = date('Y-m-d 10:00:00',strtotime('+3 day'));
      $columns_deal['camp_start'] = date('Y-m-d H:i:s'); //current time   
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
      //$columns_deal['oriprice'] = floor($value['coupon:value']);
      //$columns_deal['disper'] = $value['coupon:percent_discount'];
      $columns_deal['disprice'] = $value['Price'];
      $columns_deal['ext_image_url'] = $ext_img_url;
      
      $columns_deal['camp_cat'] = '29';//Fashion
     
     
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
      
      
      //echo '<pre>';
      //print_r($params);
      //echo '</pre>';
      //exit();
      
      $api->save_pod_item($params);
    }

 
}
?>